package com.chima.action;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import com.chima.domain.Employees;
import com.chima.domain.Job;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.service.JobService;
import com.opensymphony.xwork2.ActionSupport;
import flexjson.JSONSerializer;
import flexjson.transformer.DateTransformer;
public class JobAction extends ActionSupport {
	private String name;
	private Date creatDate;
	private Employees employee;
	private int jobstyle;
	private String core;
	private String chaungxincore;
	private int state;
	private String nanyi;
	private int id;
	private int jobId;
	private String searchdate;
	
	public String getSearchdate() {
		return searchdate;
	}
	public void setSearchdate(String searchdate) {
		this.searchdate = searchdate;
	}
	public String getChaungxincore() {
		return chaungxincore;
	}
	public void setChaungxincore(String chaungxincore) {
		this.chaungxincore = chaungxincore;
	}
	public String getNanyi() {
		return nanyi;
	}
	public void setNanyi(String nanyi) {
		this.nanyi = nanyi;
	}
	public void setCore(String core) {
		this.core = core;
	}
	public int getJobId() {
		return jobId;
	}
	public void setJobId(int jobId) {
		this.jobId = jobId;
	}
	private boolean success=false ;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getCreatDate() {
		return creatDate;
	}
	public void setCreatDate(Date creatDate) {
		this.creatDate = creatDate;
	}
	public Employees getEmployee() {
		return employee;
	}
	public void setEmployee(Employees employee) {
		this.employee = employee;
	}
	public int getJobstyle() {
		return jobstyle;
	}
	public void setJobstyle(int jobstyle) {
		this.jobstyle = jobstyle;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	private int start;// 开始页码
	private int limit; //每页显示
	private int page;//页数
	private  JobService jobService;
	private List<Job> jobs;
	
  public List<Job> getJobs() {
		return jobs;
	}
	public void setJobs(List<Job> jobs) {
		this.jobs = jobs;
	}
public JobService getJobService() {
		return jobService;
	}
	public void setJobService(JobService jobService) {
		this.jobService = jobService;
	}
public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
public String  ListJob() throws Exception{
	    Employees  existEmployee =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
	    PageRequestBean pageRequestBean = new PageRequestBean();
	    DetachedCriteria criteria = DetachedCriteria.forClass(Job.class);
	    if(searchdate==null || "".equals(searchdate)){
	    	 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	  	     Calendar  c = Calendar.getInstance();
	         c.set(Calendar.DAY_OF_YEAR, 1);//本年第一天
	         String first=sdf.format(c.getTime());
	         c.add(Calendar.YEAR, 1);
	         c.set(Calendar.DAY_OF_YEAR, 1);//本年最后一天
	         c.add(Calendar.DAY_OF_YEAR, -1);
	         String Last=sdf.format(c.getTime());
	         criteria.add(Restrictions.ge("creatDate", sdf.parse(first)));
			 criteria.add(Restrictions.le("creatDate", sdf.parse(Last)));
	    	
	    }
	    if(searchdate!=null){
	    	 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
			 int day=Integer.parseInt( searchdate.substring(5,7));
			 int year=Integer.parseInt( searchdate.substring(0,4));
			String  First =getFirstDayOfMonth(year,day);
			String  last =getLastDayOfMonth(year,day);
			criteria.add(Restrictions.ge("creatDate", sdf.parse(First)));
			criteria.add(Restrictions.le("creatDate", sdf.parse(last)));
	    }
	    if(!existEmployee.getName().equals("admin")){
	    	  criteria.add(Restrictions.eq("employee.id",existEmployee.getId() ));
	    	  pageRequestBean.setEmp(existEmployee.getId());
	    }
	    criteria.add(Restrictions.eq("jobstyle",2 ));
	    pageRequestBean.setJobstyle(2);
	    pageRequestBean.setDate(searchdate);
	    pageRequestBean.setCriteria(criteria);
		pageRequestBean.setStart(start);
		pageRequestBean.setLimit(limit);
		pageRequestBean.setPage(page);
		PageResponseBean pageResponseBean = jobService.findData(pageRequestBean);
        jobs=pageResponseBean.getRows();
        int total=0;
        total =pageResponseBean.getTotal();
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.include("*.id","*.name","*.creatDate","*.employee","*.core","*.state","*.jobstyle");
        jsonSerializer.exclude("*.class","*.professional","*.department","*.post","*.rose");
        jsonSerializer.transform(new DateTransformer("yyyy-MM-dd"),Date.class);
        String resultJson1 = jsonSerializer.deepSerialize(jobs);  
        String resultJson2 =jsonSerializer.serialize(total);
        System.out.println(resultJson2);
        String jsonString = "{total:"+resultJson2.toString()+",jobs:"+resultJson1.toString()+"}";
        System.out.print(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	
  }
public String  ListInnovativeJob() throws Exception{
    Employees  existEmployee =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
    PageRequestBean pageRequestBean = new PageRequestBean();
    DetachedCriteria criteria = DetachedCriteria.forClass(Job.class);
    if(searchdate==null || "".equals(searchdate)){
    	 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  	     Calendar  c = Calendar.getInstance();
         c.set(Calendar.DAY_OF_YEAR, 1);//本年第一天
         String first=sdf.format(c.getTime());
         c.add(Calendar.YEAR, 1);
         c.set(Calendar.DAY_OF_YEAR, 1);//本年最后一天
         c.add(Calendar.DAY_OF_YEAR, -1);
         String Last=sdf.format(c.getTime());
         criteria.add(Restrictions.ge("creatDate", sdf.parse(first)));
		 criteria.add(Restrictions.le("creatDate", sdf.parse(Last)));
    	
    }
    if(searchdate!=null){
    	 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
		 int day=Integer.parseInt( searchdate.substring(5,7));
		 int year=Integer.parseInt( searchdate.substring(0,4));
		String  First =getFirstDayOfMonth(year,day);
		String  last =getLastDayOfMonth(year,day);
		criteria.add(Restrictions.ge("creatDate", sdf.parse(First)));
		criteria.add(Restrictions.le("creatDate", sdf.parse(last)));
    }
    if(!existEmployee.getName().equals("admin")){
    	  criteria.add(Restrictions.eq("employee.id",existEmployee.getId() ));
    	  pageRequestBean.setEmp(existEmployee.getId());
    }
    criteria.add(Restrictions.eq("jobstyle",1));
    pageRequestBean.setJobstyle(1);
    pageRequestBean.setDate(searchdate);
    pageRequestBean.setCriteria(criteria);
	pageRequestBean.setStart(start);
	pageRequestBean.setLimit(limit);
	pageRequestBean.setPage(page);
	PageResponseBean pageResponseBean = jobService.InnovativeJob(pageRequestBean);
    jobs=pageResponseBean.getRows();
    int total=0;
    total =pageResponseBean.getTotal();
	// 输出json内容到客户端
    JSONSerializer jsonSerializer = new JSONSerializer();
    jsonSerializer.include("*.id","*.name","*.creatDate","*.employee","*.core","*.state","*.jobstyle");
    jsonSerializer.exclude("*.class","*.professional","*.department","*.post","*.rose");
    jsonSerializer.transform(new DateTransformer("yyyy-MM-dd"),Date.class);
    String resultJson1 = jsonSerializer.deepSerialize(jobs);  
    String resultJson2 =jsonSerializer.serialize(total);
    System.out.println(resultJson2);
    String jsonString = "{total:"+resultJson2.toString()+",jobs:"+resultJson1.toString()+"}";
    System.out.print(jsonString);
	ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
	ServletActionContext.getResponse().getWriter().print(jsonString);
	return NONE;

}
 public void addJob() throws Exception{
			 Employees  existEmployee =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
			    if(jobstyle==1){
			    	Job job =new Job();
					job.setName(name);
					job.setCore(Double.parseDouble(chaungxincore));
					job.setJobstyle(jobstyle);
					job.setCreatDate(creatDate);
					job.setState(state);
					job.setNanyi(0);
					job.setEmployee(existEmployee);
				    jobService.saveJobs(job);
			    }if(jobstyle==2){
			    	Job job =new Job();
					job.setName(name);
					job.setCore(Double.parseDouble(core));
					job.setJobstyle(jobstyle);
					job.setCreatDate(creatDate);
					job.setState(state);
					job.setNanyi(Integer.parseInt(nanyi));
					job.setEmployee(existEmployee);
				    jobService.saveJobs(job);
			    }
			    
					
			this.success=true;
			JSONSerializer jsonSerializer = new JSONSerializer();
			String resultJson =jsonSerializer.serialize(success);
			String jsonString = "{success:"+resultJson.toString()+"}";
			System.out.println(jsonString);
			ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(jsonString);
		}
		public void editJob() throws Exception{
			    Employees  existEmployee =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
			    if(jobstyle==1){
			    	Job job =new Job();
					job.setName(name);
					job.setCore(Double.parseDouble(chaungxincore));
					job.setJobstyle(jobstyle);
					job.setCreatDate(creatDate);
					job.setState(state);
					job.setNanyi(0);
					job.setEmployee(existEmployee);
					job.setId(id);
					 jobService.updateJobs(job);
			    }if(jobstyle==2){
			    	Job job =new Job();
					job.setName(name);
					job.setCore(Double.parseDouble(core));
					job.setJobstyle(jobstyle);
					job.setCreatDate(creatDate);
					job.setState(state);
					job.setNanyi(Integer.parseInt(nanyi));
					job.setEmployee(existEmployee);
					job.setId(id);
				    jobService.updateJobs(job);
			    }
			    	
			this.success=true;
			JSONSerializer jsonSerializer = new JSONSerializer();
			String resultJson =jsonSerializer.serialize(success);
			String jsonString = "{success:"+resultJson.toString()+"}";
			System.out.println(jsonString);
			ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(jsonString);
		}
		public String delete(){
				jobService.deleteJobs(id);
			return NONE;
		}
	public String fabu() throws Exception{
		  Job job=jobService.findJobById(id);
		  if(job.getState()==1){
			    job.setState(2);
				jobService.updateJobs(job);
			  	this.success=true;
				JSONSerializer jsonSerializer = new JSONSerializer();
				String resultJson =jsonSerializer.serialize(success);
				String jsonString = "{success:"+resultJson.toString()+"}";
				System.out.println(jsonString);
				ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
				ServletActionContext.getResponse().getWriter().print(jsonString);  	
		  }else{
			  throw new RuntimeException("项目正在办理或已经结束");
		  }
		  return NONE;
	}
	public String quxiaofabu() throws Exception{
		  Job job=jobService.findJobById(id);
		  if(job.getState()==2){
			    job.setState(1);
				jobService.updateJobs(job);
			  	this.success=true;
				JSONSerializer jsonSerializer = new JSONSerializer();
				String resultJson =jsonSerializer.serialize(success);
				String jsonString = "{success:"+resultJson.toString()+"}";
				System.out.println(jsonString);
				ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
				ServletActionContext.getResponse().getWriter().print(jsonString);  	
		  }else{
			  throw new RuntimeException("项目正已经结束");
		  }
		  return NONE;
	}
	//添加工作详情
	public String  addJobdetail () throws Exception{
		  Job job=jobService.findJobById(jobId);
		  if(job.getState()==1){
			  	this.success=true;
				JSONSerializer jsonSerializer = new JSONSerializer();
				String resultJson =jsonSerializer.serialize(success);
				String jsonString = "{success:"+resultJson.toString()+"}";
				System.out.println(jsonString);
				ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
				ServletActionContext.getResponse().getWriter().print(jsonString);  	
		  }else{
			  throw new RuntimeException("项目正在办理或已经结束");
		  }
		  return NONE;
	}
   public String  saercherJob (){
	   
	  
	   return SUCCESS;
	}
   
   
   /***
	  * 得到某年某月的第一天
	  * @param year
	  * @param month
	 */
	public String getFirstDayOfMonth(int year, int month)  {
		 
		  Calendar cal = Calendar.getInstance();
		  cal.set(Calendar.YEAR, year);
		  cal.set(Calendar.MONTH, month-1);
		  cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));	 
		
		  return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
	}
   /**
	  * 得到某年某月的最后一天
	  *
	  * @param year
	  * @param month
	*/
	 public  String getLastDayOfMonth(int year, int month) throws Exception {
		 
		  Calendar cal = Calendar.getInstance();
		  cal.set(Calendar.YEAR, year);
		  cal.set(Calendar.MONTH, month-1);
		  cal.set(Calendar.DAY_OF_MONTH, 1);
		  int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		  cal.set(Calendar.DAY_OF_MONTH, value);
		 	
		  return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); 
		 }
}

